package com.controller;

import com.domain.Orders;
import com.github.pagehelper.PageInfo;
import com.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 项目名称：ssm21
 * 类 名 称：OrdersController
 * 类 描 述：TODO
 * 创建时间：2020-01-16 12:03
 */
@Controller
@RequestMapping("/orders")
@SuppressWarnings("unchecked")
public class OrdersController {
    @Autowired
    private IOrderService iOrderService;

    @RequestMapping("/findAll.do")
    @Secured("ROLE_ADMIN")
    ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") Integer page, @RequestParam(name = "size", required = true, defaultValue = "4") Integer size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Orders> all = iOrderService.findAll(page, size);
        // PageInfo就是一个分页Bean
        PageInfo pageInfo = new PageInfo(all);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }


    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam(name = "id", required = true) String ordersId) throws Exception {
        ModelAndView mv = new ModelAndView();
        Orders orders = iOrderService.findById(ordersId);
        mv.addObject("orders", orders);
        mv.setViewName("orders-show");
        return mv;
    }
}
